package com.office.config;

//import com.office.Filter.JwtAuthenticationTokenFilter;
import com.office.Filter.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 *
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

   @Autowired
    JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Resource
    private ChatAuthenticationFailureHandler failureHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests()
                .antMatchers("/","/static/**","/user/login","/swagger-ui.html","/swagger-resources/**",
                        "/webjars/**","/v2/**","/api/**",
                        "/v2/api-docs",
                        "/swagger-resources/configuration/ui",
                        "/swagger-resources",
                        "/swagger-resources/configuration/security",
                        "/swagger-ui.html")
               .permitAll()
                .anyRequest().authenticated()
                // 其他地址的访问均需验证权限
                .and()
                .formLogin()
                .usernameParameter("name")
                .passwordParameter("password")
                .loginPage("/login.html").loginProcessingUrl("/user/login").permitAll()
                .failureHandler(failureHandler)//  登录页
//                .successForwardUrl("/main.html") // 登入成功后，跳转至指定页面
                .defaultSuccessUrl("/main.html")   // 访问指定页面，用户未登入，跳转至登入页面，如果登入成功，跳转至用户访问指定页面，用户访问登入页面，默认的跳转页面
//                .failureUrl("/login.html").permitAll()
//                .and()
//                .logout().logoutUrl("/logout")
//                .logoutSuccessUrl("/logoutSuccess").permitAll()
        ;

        http.authorizeRequests()
                .and().headers().frameOptions().sameOrigin();//更改X-Frame-Options为SAMEORIGIN，会导致iframe框架不能显示内容

//        把token校验过滤器添加到过滤器链中
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}